// pages/enquiry/enquiry.js
const app = getApp()
const constant = require('../../constant');
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['', '', '请选择'],
    objectIndex:0,
    objectArray: [
      {
        id: 1,
        name: '一周内'
      },
      {
        id: 2,
        name: '一月内'
      },
      {
        id: 3,
        name: '三月内'
      },
      {
        id: 4,
        name: '三月以上'
      }
    ],
    goods_id: '',
    baseinfo: [],

    multiIndex: [0, 0],
    multiArray: [],
    objectMultiArray: [],
    name: '',
    phone: ''
  },
  getMultiArray: function() {
    let that = this;
    app.get('Site/getAreaMultiArray', {
    }, function (res) {
      if (res.code == 200 && res.data != '') {
        that.setData({
          multiArray: res.data,
        })
      }
    })
  },
  getObjectMultiArray: function () {
    let that = this;
    app.get('Site/getAreaObjectMultiArray', {
      goods_id: that.data.goods_id
    }, function (res) {
      if (res.code == 200 && res.data != '') {
        that.setData({
          objectMultiArray: res.data,
        })
      }
    })
  },
  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变，值为', this.data.multiArray[0][e.detail.value[0]], this.data.multiArray[1][e.detail.value[1]])
    this.setData({ 
      "multiIndex[0]": e.detail.value[0],
      "multiIndex[1]": e.detail.value[1]
    })
  },
  bindMultiPickerColumnChange: function (e) {
    var list = [];
    var that = this;
    switch (e.detail.column) {
      case 0:
        list = []
        for (var i = 0; i < that.data.objectMultiArray.length; i++) 
        {
          if (that.data.objectMultiArray[i].pid == that.data.objectMultiArray[e.detail.value].id) {
            list.push(that.data.objectMultiArray[i].name)
          }
        }
        that.setData({
          "multiArray[1]": list,
          "multiIndex[0]": e.detail.value,
          "multiIndex[1]": 0 
        })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      goods_id: options.goods_id
    })
    this.getTruckBaseinfo();
    this.getMultiArray();
    this.getObjectMultiArray();
  },
  getTruckBaseinfo() {
    let that = this;
    app.get('Goods/gettruckbaseinfo', {
      goods_id: that.data.goods_id
    }, function (res) {
      if (res.code == 200 && res.data != '') {
        res.data.goods_img = constant.config.host + res.data.goods_img;
        that.setData({
          baseinfo: res.data,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //省市区选择器：
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  bindPickerChange(e){
    this.setData({
      objectIndex: e.detail.value
    })
  },
  inputName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  inputPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  enquiryFree: function() {
    let that = this;
    if (util.isEmpty(that.data.name)) {
      wx.showToast({
        title: '姓名不能为空', duration: 1000
      })
      return;
    }
    if (util.isEmpty(that.data.phone)) {
      wx.showToast({
        title: '手机不能为空', duration: 1000
      })
      return;
    }
    app.get('User/reqfloorprice', {
      province_name: that.data.multiArray[0][that.data.multiIndex[0]],
      city_name: that.data.multiArray[1][that.data.multiIndex[1]],
      time: that.data.objectIndex,
      name: that.data.name,
      goods_id: that.data.goods_id,
      phone: that.data.phone
    }, function (res) {
      wx.showToast({
        title: res.message,
        icon: 'none',
        duration: 500,
        success() {
          setTimeout(function () {
            wx.navigateBack({
              detail: 1
            })
          }, 500)
        }
      })
    })
  }
})